const vscode = require("vscode");
const GlobalVriable = require("../utils/globalVriable");

/**
 * 悬停提示
 * @param {any} document
 * @param {any} position
 * @param {any} token
 */
// eslint-disable-next-line no-unused-vars
function provideHover(document, position, token) {
    const fileName = document.fileName;
    const word = document.getText(document.getWordRangeAtPosition(position));

    // 判断文件类型
    if (!/\\.*\.(Q|qml)$/.test(fileName)) { return; }

    // 悬停提示
    let functionInfoList = GlobalVriable.getAjjlFunctionLibrary(document);
    let definitionReg = new RegExp(`(Function|Sub)\\s*${word}\\s*\\(`, "g");
    for (const infoNum in functionInfoList) {
        if (functionInfoList.hasOwnProperty(infoNum)) {
            let infoObj = functionInfoList[infoNum];
            if (definitionReg.test(infoObj["content"])) {
                for (const infoNum in infoObj["infos"]) {
                    if (infoObj["infos"].hasOwnProperty(infoNum) && infoObj["infos"][infoNum]["name"] === word) {
                        let hoverText = `* 函数库：${infoObj["class"]}${infoObj["postfix"]}\n`;
                        hoverText += `* ${infoObj["infos"][infoNum]["type"]}  `;
                        hoverText += `${infoObj["infos"][infoNum]["name"]}`;
                        hoverText += `\(${infoObj["infos"][infoNum]["params"].join(", ")}\)`;
                        return new vscode.Hover(hoverText);
                    }
                }
            }
        }
    }
}

/**
 * 注册悬停提示
 * @param {any} context
 */
module.exports = function(context) {
    context.subscriptions.push(vscode.languages.registerHoverProvider("q", { provideHover }));
};